<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Userapi_service {

// 	$this 只能在你的控制器、模型或视图中直接使用，如果你想在你自己的类中使用 CodeIgniter 类，你可以像下面这样做：

// 首先，将 CodeIgniter 对象赋值给一个变量:

// $CI =& get_instance();
// 一旦你把 CodeIgniter 对象赋值给一个变量之后，你就可以使用这个变量来 代替 $this
// 	$CI =& get_instance();

// $this->CI->load->helper('url');
	// $this->CI->load->library('session');
// $CI->load->library('session');
// $CI->config->item('base_url');
	
	protected $CI;
	// 如果要往这个类库里面传参数，这里就要构造函数 并且接收数组
	public function __construct()
    {	
        $this->CI =& get_instance(); //将 CodeIgniter 对象赋值给一个变量:
        $this->CI->load->model('model_m');// 通用模型 实例 增删改查 的模型 
		$this->CI->load->model('userapi_m');// 用户表 实例 增删改查 的模型
		$this->CI->load->model('appAuth_m');// 第三方 实例 增删改查 的模型
    }
    
	public function user_api_get($get)
	{	
		$user_where = array('user_id'=>$get['user_id']); //where
		if(empty($get['user_id'])){
			$data['error'] = error(4006);
			return $data;
		}else{
	        $user_weixin_where = array('user_id'=>$get['user_id']); //where
	        $data['success'] = success(4000);
	    	$data['user'] = $this->CI->userapi_m->select($user_where);
	    	$data['weixin'] = $this->CI->appAuth_m->select($user_weixin_where);
	    	return $data;
		}

		
	}


	//通过账号密码登录
	public function getUserByUsernameAndPassword($user_where)
	{
		if(empty($user_where['username'])||empty($user_where['password'])){
			$data['error'] = error(4006);
			return $data;
		}else{
	        $data['success'] = success(4000);
	    	$data['user'] = $this->CI->userapi_m->select($user_where);
	    	$data['weixin'] = $this->CI->model_m->select('users_weixin',$user_weixin_where);
	    	return $data;
		}
		
	}

	//查询某绑定是否用过
	public function getUserByinfo($field,$value)
	{
		if(empty($field)||empty($value)){
			$data['error'] = error(4006);
			return $data;
		}else{
			$where[$field]=$value;
	        $data['success'] = success(4000);
	    	$data['user'] = $this->CI->userapi_m->select($where);
	    	$user_weixin_where = array('user_id'=>$data['user'][0]['user_id']); //where
	    	$data['weixin'] = $this->CI->model_m->select('users_weixin',$user_weixin_where);
	    	return $data;
		}
		
	}

	public function user_api_post($post)
	{	
			
		$user_where = array('username'=>$post['username']); //where
		$user_old = $this->CI->userapi_m->select($user_where); //如果用户已存在

		if($user_old){
			$user_old_where = array('user_id'=>$user_old[0]['user_id']); //where
	        $user_old_weixin_where = array('user_id'=>$user_old[0]['user_id']); //where
	        $data['error'] = error(5000);
	    	$data['user'] = $this->CI->userapi_m->select($user_old_where);
	    	$data['weixin'] = $this->CI->model_m->select('users_weixin',$user_old_weixin_where);
	    	return $data;
		}else{

			$user_last = $this->CI->userapi_m->select('','user_id desc','1');
			$data['user_id'] = $user_last[0]['user_id']+1;
			$data['username'] = $this->CI->input->post('username');  
			$data['password'] = MD5($this->CI->input->post('password'));  //MD5	
			$data['mobile'] = $this->CI->input->post('mobile');  
			$data['email'] = $this->CI->input->post('email'); 

			if(empty($data['username'])){
	    		$data['error'] = error(4007);
		    	return $data['error'];
		    	exit;
			}

			if(empty($data['password'])){
	    		$data['error'] = error(4007);
		    	return $data['error'];
		    	exit;
			}

			if(empty($data['mobile'])){
	    		$data['error'] = error(4007);
		    	return $data['error'];
		    	exit;
			}
					
			//添加用户
			$new_user = $this->CI->userapi_m->insert($data);

			//返回新添加的用户的信息
			$user_data_where = array('user_id'=>$data['user_id']); //where
	        $user_weixin_where = array('user_id'=>$data['user_id']); //where
	        $user_data['success'] = success(4000);
	    	$user_data['user'] = $this->CI->userapi_m->select($user_data_where);
	    	$user_data['weixin'] = $this->CI->model_m->select('users_weixin',$user_weixin_where);
	    		
	    	if($user_data['user'][0]['user_id']){
	    		return $user_data;
	    	}
		}
	}


	public function user_api_put($put)
	{	
		$user_where = array('user_id'=>$put['user_id']); //where
		$user = $this->CI->userapi_m->select($user_where);

		if(empty($put['user_id'])){
			$data['error'] = error(4006);
	    	return $data['error'];
		}else{
			
			//修改用户
			$data['username'] = $put['username'];
			$data['password'] = $put['password'];
			$data['mobile'] = $put['mobile'];
			$data['email'] = $put['email'];

			$user_update = $this->CI->userapi_m->update($user_where,$data);
				
	        $user_weixin_where = array('user_id'=>$put['user_id']); //where
	        $user_data['success'] = success(4000);
	    	$user_data['user'] = $this->CI->userapi_m->select($user_where);
	    	$user_data['weixin'] = $this->CI->model_m->select('users_weixin',$user_weixin_where);
	    				
	    	if($user_data['user'][0]['user_id']){
	    		return $user_data;
	    	}
		}	
		
	}
	
	
	public function user_api_delete($delete)
	{	
		$user_where = array('user_id'=>$delete['user_id']); //where
		$user = $this->CI->userapi_m->select($user_where);

		if(empty($delete['user_id'])){
			$data['error'] = error(4006);
	    	return $data['error'];
		}else{
			//删除用户
			$user_delete = $this->CI->userapi_m->delete($user_where);
	        $data['success'] = success(4000);
	    	return $data;
		}
	}
}